package ar.edu.utn.frsf.cidisi.upcolbpip.upgen;

import java.util.Vector;

import ar.edu.utn.frsf.cidisi.upcolbpip.UPColBPIPModel;
import ar.edu.utn.frsf.cidisi.upcolbpip.UpcolbpipFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.b2bcollaborations.B2BCollaboration;
import ar.edu.utn.frsf.cidisi.upcolbpip.b2bcollaborations.B2bcollaborationsFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.collaborativebusinessprocesses.CollaborativeBusinessProcess;
import ar.edu.utn.frsf.cidisi.upcolbpip.collaborativebusinessprocesses.CollaborativebusinessprocessesFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.And;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.ControlFlowSegment;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Termination;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Xor;

public class CreateModel {

	private UPColBPIPModel modelUpcolbpip;
	private Vector<String> protocol;
	private int i,intprotID=1; // 0 por 1 ?
	
	
	public CreateModel() {
	
		modelUpcolbpip = UpcolbpipFactory.eINSTANCE.createUPColBPIPModel();
		B2BCollaboration b2bCollaboration = B2bcollaborationsFactory.eINSTANCE.createB2BCollaboration();

		modelUpcolbpip.setB2bCollaboration(b2bCollaboration);

		CollaborativeBusinessProcess collaborativeBusinessProcess =  CollaborativebusinessprocessesFactory.eINSTANCE.createCollaborativeBusinessProcess();
		b2bCollaboration.getProcesses().add( collaborativeBusinessProcess );

	}
	
	public UPColBPIPModel getModelUpcolbpip() {
		return modelUpcolbpip;
	}
	
	
	public void addInteractionProtocols(Vector< Vector<String> > newProtocols) {
		
		for(int j=0;j<newProtocols.size();j++) {
			
			addInteractionProtocol(newProtocols.get(j));
			
		}
		
	}
	
	public void addInteractionProtocol(Vector<String> newProtocol) {

		protocol = newProtocol;
		
		InteractionProtocol interactionProtocol = InteractionprotocolsFactory.eINSTANCE.createInteractionProtocol();
		
		newInteractionProtocol( interactionProtocol );
		interactionProtocol.setName(intprotID+"");
		intprotID++;
		modelUpcolbpip.getB2bCollaboration()
		.getProcesses().get(0)
		.getInteractionProtocols()
		.add( interactionProtocol );

	}
	
	public void newInteractionProtocol(InteractionProtocol interactionProtocol) {
		
		ControlFlowSegment anterior = null;
		InteractionPath intPath = null;
		
		for(i=0;i<protocol.size();i++) {
			if(protocol.get(i) == "Child") {
				
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
			
					addNewChildInteraction(intPath,interactionProtocol);
			
			}
				
			else if(protocol.get(i) == "And") { 
				anterior = InteractionprotocolsFactory.eINSTANCE.createAnd();
				
				if((i+1)<protocol.size() && protocol.get(i+1) != "Child" || (i+1)==protocol.size() ) {
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
				}
				interactionProtocol.getIpElement().add(anterior);
				
			}
			else if(protocol.get(i) == "Cancel") { 
				anterior = InteractionprotocolsFactory.eINSTANCE.createCancel();
				
				if((i+1)<protocol.size() && protocol.get(i+1) != "Child" || (i+1)==protocol.size() ) {
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
				}
				interactionProtocol.getIpElement().add(anterior);
				
			}
			else if(protocol.get(i) == "Xor") {
				anterior = InteractionprotocolsFactory.eINSTANCE.createXor();
				if((i+1)!=protocol.size() && protocol.get(i+1) != "Child" || (i+1)==protocol.size() ) {
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
				}
				interactionProtocol.getIpElement().add(anterior);
				
			}
			else if(protocol.get(i) == "Cancel") {
				anterior = InteractionprotocolsFactory.eINSTANCE.createCancel();
				interactionProtocol.getIpElement().add(anterior);
				
			}
			else if(protocol.get(i) == "InteractionPath") {
				
				intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
				anterior.getInteractionPath().add(intPath);
				
			}
			else if(protocol.get(i) == "Termination") {
		
				interactionProtocol.getIpElement().add(InteractionprotocolsFactory.eINSTANCE.createTermination());
			}
				
			else if(protocol.get(i) == "BusinessMesage") {
				interactionProtocol.getIpElement().add(InteractionprotocolsFactory.eINSTANCE.createBusinessMessage());
			}
				
			
		}
		
	}

	
	public void addNewChildInteraction(InteractionPath padre,InteractionProtocol interactionProtocol) {
		
		i++;
		
		ControlFlowSegment anterior = null;
		InteractionPath intPath = null;
		
		for(;i<protocol.size();i++) {
			if(protocol.get(i) == "Child") {
				
				intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
				anterior.getInteractionPath().add(intPath);
		
				addNewChildInteraction(intPath,interactionProtocol);
				
			}
			else if(protocol.get(i) == "And") { 
				anterior = InteractionprotocolsFactory.eINSTANCE.createAnd();
				if((i+1)!=protocol.size() && protocol.get(i+1) != "Child" || (i+1)==protocol.size() ) {
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
				}
				padre.getElement().add(anterior);
				
			}
			else if(protocol.get(i) == "Cancel") { 
				anterior = InteractionprotocolsFactory.eINSTANCE.createCancel();
				if((i+1)!=protocol.size() && protocol.get(i+1) != "Child" || (i+1)==protocol.size() ) {
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
				}
				padre.getElement().add(anterior);
				
			}
			else if(protocol.get(i) == "Xor") {
				anterior = InteractionprotocolsFactory.eINSTANCE.createXor();
				if((i+1)!=protocol.size() && protocol.get(i+1) != "Child" || (i+1)==protocol.size() ) {
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
					intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
					anterior.getInteractionPath().add(intPath);
				}
				padre.getElement().add(anterior);
				
			}
			else if(protocol.get(i) == "Termination") {
			
				padre.getElement().add( InteractionprotocolsFactory.eINSTANCE.createTermination());
			}
			else if(protocol.get(i) == "InteractionPath") {
				
				intPath =	InteractionprotocolsFactory.eINSTANCE.createInteractionPath();
				anterior.getInteractionPath().add(intPath);
				
			}
			else if(protocol.get(i) == "Cancel") {
				anterior = InteractionprotocolsFactory.eINSTANCE.createCancel();
				padre.getElement().add(anterior);
				
			}	
			else if(protocol.get(i) == "BusinessMesage") {
				
				padre.getElement().add(InteractionprotocolsFactory.eINSTANCE.createBusinessMessage());
			}
			else if(protocol.get(i) == "ChildOut") { break;}
			
		}
		
	}
}
